package org.linlinjava.litemall.mechanismadmin.web;

import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallVoucherOrder;
import org.linlinjava.litemall.db.service.LitemallMechanismUserService;
import org.linlinjava.litemall.db.service.LitemallOrderVoucherService;
import org.linlinjava.litemall.mechanismadmin.annotation.LoginAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mechanismadmin/payCode")
public class MechanismPayCodeController {


    @Autowired
    private LitemallOrderVoucherService  voucherOrderService;
    @Autowired
    private LitemallMechanismUserService mechanismUserService;
    @GetMapping("/list")
    public Object list(
            @LoginAdmin Integer adminId,
            String payCodeSn
           ) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        if(payCodeSn != null && payCodeSn !="") {
            payCodeSn  = "DJQ"+ payCodeSn.substring(3,payCodeSn.length());

        }
        LitemallVoucherOrder voucherOrder = voucherOrderService.queryOrderInfoByOrderSn(payCodeSn);
        if(voucherOrder==null){
            return ResponseUtil.badArgument();
        }
        Map<String, Object> data = new HashMap<>();
        data.put("items", voucherOrder);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/totalList")
    public Object totalList(
            @LoginAdmin Integer adminId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            Integer userId) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        Integer mechanismId =mechanismUserService.findMechanismIdByUserId(adminId);
        if(mechanismId==null){
            return ResponseUtil.badArgument();
        }
        List<LitemallVoucherOrder> payCodeTotalList = voucherOrderService.queryOrderListBymechanismId(mechanismId,page,limit);
        long total = PageInfo.of(payCodeTotalList).getTotal();
        Map<String, Object> data = new HashMap<>();
        data.put("items", payCodeTotalList);
        data.put("total", total);
        return ResponseUtil.ok(data);
    }
    @GetMapping("/changePayCodeState")
    public Object changePayCodeState(
            @LoginAdmin Integer adminId,
            Integer userId,
            String payCodeSn) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        int  issuccess = voucherOrderService.updateStateAndNumberByOrderSn(payCodeSn);
        Map<String, Object> data = new HashMap<>();
        data.put("items", issuccess);
        return ResponseUtil.ok(data);
    }

}
